<?php
namespace app\controller;

use app\BaseController;
use app\model\Dictionary;
use think\db\exception\DbException;
use think\exception\ValidateException;

class Import extends BaseController {
    public function cantonese_dictionary() {
        $content = file_get_contents('http://localhost/CantoneseDictionary.json');
        $dictionary = json_decode($content, true);
        foreach ($dictionary as $chars => $prons) {
            $chars_array = mb_strlen($chars) == 1 ? [$chars] : mb_str_split($chars);
            foreach ($prons as $complete_pron => $frequency) {
                $prons_array = explode(' ', $complete_pron);
                $i = 0;
                foreach ($prons_array as $char_pron) {
                    $char_pron = str_replace('!', '', $char_pron);
                    $char_pron = trim($char_pron);
                    $pron = Dictionary::find_pron($char_pron);
                    $tone = Dictionary::find_tone($char_pron);
                    $improved_pron = str_replace('aa', 'a', $pron);
                    $improved_pron = str_replace('ly', 'l', $improved_pron);
                    $improved_pron = str_replace('jy', 'y', $improved_pron);
                    $improved_pron = str_replace('dy', 'd', $improved_pron);
                    $improved_pron = str_replace('zy', 'z', $improved_pron);
                    $improved_pron = str_replace('ty', 't', $improved_pron);
                    $improved_pron = str_replace('sy', 's', $improved_pron);
                    $improved_pron = str_replace('cy', 'c', $improved_pron);
                    $improved_pron = str_replace('ky', 'k', $improved_pron);
                    $improved_pron = str_replace('gy', 'g', $improved_pron);
                    $improved_pron = str_replace('ny', 'n', $improved_pron);
                    $improved_pron = str_replace('gw', 'gu', $improved_pron);
                    $improved_pron = str_replace('kw', 'ku', $improved_pron);
                    if (str_ends_with($improved_pron, 'm')) {
                        $improved_pron = substr($improved_pron, 0, strlen($improved_pron) - 1) . 'n';
                    }
                    if (str_ends_with($improved_pron, 'ng') && mb_strlen($improved_pron) > 2) {
                        $improved_pron = str_replace('ng', 'n', $improved_pron);
                    }
                    if (str_ends_with($improved_pron, 'k') ||
                        str_ends_with($improved_pron, 'p') ||
                        str_ends_with($improved_pron, 't')) {
                        $improved_pron = substr($improved_pron, 0, mb_strlen($improved_pron) - 1);
                    }
                    if (str_starts_with($improved_pron, 'j')) {
                        $improved_pron = str_replace('j', 'y', $improved_pron);
                    }
                    $improved_pron = trim($improved_pron);
                    $improved_tone = Dictionary::improved_tone($tone);
                    try {
                        $data = [
                            'traditional_char' => $chars_array[$i],
                            'simplified_char' => Dictionary::convert_to_simplified_char($chars_array[$i]),
                            'original_pron' => $pron,
                            'original_tone' => $tone,
                            'complete_pron' => $char_pron,
                            'improved_pron' => $improved_pron,
                            'improved_tone' => $improved_tone,
                            'improved_complete_pron' => $improved_pron . $improved_tone,
                            'weigh' => $frequency
                        ];
                        validate(\app\validate\Dictionary::class)->check($data);
                        (new Dictionary())->save($data);
                        $i++;
                    } catch (ValidateException $e) {
                        echo $chars_array[$i] . ' ' . $e->getMessage() . '<br>';
                        continue;
                    } catch (DbException $e) {
                        echo $chars_array[$i] . ' ' . $e->getMessage() . '<br>';
                        continue;
                    } catch (\Exception $e) {
                        echo $e->getMessage() . '<br>';
                        continue;
                    }
                }
            }
        }
    }
}